from logging import Logger

import requests
import logging
from sdk.views import HOST, CORPID, AGENTID, SECRET, getAccessToken

logger: Logger = logging.getLogger("log")


def getTemplateDetail(template_id):
    code = 0
    msg = "success"
    data = {}
    apiName = "获取审批模板详情"
    logger.info(f"接口>>>{apiName}>>>开始调用")
    ACCESS_TOKEN = getAccessToken()
    if ACCESS_TOKEN == False:
        code = 1
        msg = "获取access_token失败"
        apiOutput = f"1.code<{code}>; 2.msg<{msg}>;"
        logger.info(f"接口>>>{apiName}>>>执行失败>>>{apiOutput}")
        return {"code": code, "msg": msg, "data": data}
    url = HOST + "oa/gettemplatedetail?access_token=" + ACCESS_TOKEN
    d = {"template_id": template_id}
    apiInput = f"1.access_token<{ACCESS_TOKEN}>;2.template_id<{template_id}>;"
    logger.info(f"接口>>>{apiName}>>>入参>>>{apiInput}")
    try:
        response = requests.post(url, json=d)
        responseJson = response.json()
        print("responseJson")
        print(responseJson)
        if responseJson["errcode"] != 0:
            code = responseJson["errcode"]
            msg = responseJson["errmsg"]
            apiOutput = f"1.code<{code}>; 2.msg<{msg}>;"
            logger.info(f"接口>>>{apiName}>>>执行失败>>>{apiOutput}")
        else:
            data["userid"] = responseJson["userid"]
            apiOutput = f"1.code<{code}>;2.msg<{msg}>;"
            logger.info(f"接口>>>{apiName}>>>执行成功>>>{apiOutput}")
    except Exception as e:
        print("未知异常")
        print(e)
        code = 1
        msg = "未知异常"
        apiOutput = f"1.code<{code}>; 2.msg<{msg}>;"
        logger.info(f"接口>>>{apiName}>>>执行失败>>>{apiOutput}")
    return code, msg, data


def createDailyForm(clientname, userids):
    code = 0
    msg = "success"
    data = {}
    apiName = "创建收集表"
    logger.info(f"接口>>>{apiName}>>>开始调用")
    ACCESS_TOKEN = getAccessToken()
    if not ACCESS_TOKEN:
        code = 1
        msg = "获取access_token失败"
        apiOutput = f"1.code<{code}>; 2.msg<{msg}>;"
        logger.info(f"接口>>>{apiName}>>>执行失败>>>{apiOutput}")
        return {"code": code, "msg": msg, "data": data}
    url = HOST + "wedoc/create_form?access_token=" + ACCESS_TOKEN
    d = {
        "form_info": {
            "form_title": clientname + "拜访表",
            "form_desc": "本表用于收集客户拜访信息，由系统生成",
            "form_question": {
                "items": [
                    {
                        "question_id": 1,
                        "title": "拜访情况",
                        "pos": 1,
                        "reply_type": 1,
                        "must_reply": True,
                    },
                    {
                        "question_id": 2,
                        "title": "打卡",
                        "pos": 2,
                        "reply_type": 5,
                        "must_reply": True,
                        "question_extend_setting": {
                            "location_setting": {
                                "location_type": 4
                            }
                        }
                    },
                    {
                        "question_id": 3,
                        "title": "拍照",
                        "pos": 3,
                        "reply_type": 9,
                        "must_reply": True,
                        "question_extend_setting": {
                            "image_setting": {
                                "camera_only": True,
                            }
                        }
                    }
                ]
            },
            "form_setting": {
                "fill_out_auth": 1,
                "fill_in_range": {
                    "userids": userids
                },
                "allow_multi_fill": False,
                "timed_finish": 0,
                "can_anonymous": False,
                "can_notify_submit": True
            }
        }
    }
    apiInput = f"1.access_token<{ACCESS_TOKEN}>;2.clientname<{clientname}>;3.userids<{userids}>;"
    logger.info(f"接口>>>{apiName}>>>入参>>>{apiInput}")
    try:
        response = requests.post(url, json=d)
        responseJson = response.json()
        print("responseJson")
        print(responseJson)
        if responseJson["errcode"] != 0:
            code = responseJson["errcode"]
            msg = responseJson["errmsg"]
            apiOutput = f"1.code<{code}>; 2.msg<{msg}>;"
            logger.info(f"接口>>>{apiName}>>>执行失败>>>{apiOutput}")
        else:
            formid = responseJson["formid"]
            data["formid"] = formid
            apiOutput = f"1.code<{code}>;2.msg<{msg}>;3.formid<{formid}>;"
            logger.info(f"接口>>>{apiName}>>>执行成功>>>{apiOutput}")
    except Exception as e:
        print("未知异常")
        print(e)
        code = 1
        msg = "未知异常"
        apiOutput = f"1.code<{code}>;2.msg<{msg}>;"
        logger.info(f"接口>>>{apiName}>>>执行失败>>>{apiOutput}")
    return {"code": code, "msg": msg, "data": data}
